package com.example.base.bean

/**
 * @Description
 * @department 城市元宇宙组
 * @Author niushilong
 * @Date 2024-04-05 13:32
 */
sealed class ResponseResult<out T>{

    data class Success<out T>(val data: T?): ResponseResult<T>()

    data class Error(val exception: Exception): ResponseResult<Nothing>()

    // 检查响应结果
    fun checkResult(success: (T?) -> Unit, error: (String?) -> Unit){
        if (this is Success){
            success(data)
        }else if (this is Error){
            error(exception.message)
        }
    }

    // 检查是否成功
    fun checkSuccess(success: (T?) -> Unit){
        if (this is Success){
            success(data)
        }
    }

    override fun toString(): String {
        return when(this) {
            is Success -> "Success[data=$data]"
            is Error -> "Error[exception=$exception]"
        }
    }
}
